//
// Created by denglibin on 2021/4/10 0010.
//

#include "bg.h"
#include "SDL_image.h"
/**
 * 创建山
 * @param bg
 */
static void initMountains(Background* bg);
/**
 * 创建路面
 * @param bg
 */
static void initRoadPositions(Background* bg);



Background * BG_createBg(const char* imgPath,GameWindow * gameWindow){
    SDL_Surface  * surface = IMG_Load(imgPath);
    if (surface == NULL){
        SDL_Log("Can not load image %s\n", SDL_GetError());
        exit(1);
    }
    SDL_Texture * imgTexture = SDL_CreateTextureFromSurface(gameWindow->sdl_win_renderer, surface);
    if (imgTexture == NULL){
        SDL_Log("Can not create texture %s\n", SDL_GetError());
        exit(1);
    }
    SDL_FreeSurface(surface);//清理
    Background * bg = (Background*)calloc(1, sizeof (Background));
    bg->texture = imgTexture;
    bg->width = 1200;
    initMountains(bg);
    initRoadPositions(bg);
    return bg;
}
void BG_freeBg(Background* bg){
    SDL_DestroyTexture(bg->texture);
    //free mountain
    for(int i = 0; i < sizeof(bg->mountains) / sizeof(Mountain*); i++){
        MOUN_freeMountain(bg->mountains[i]);
    }
    //free road_position
    for(int i = 0; i < sizeof(bg->roadPositions) / sizeof(RoadPosition*);i++){
        RP_freeRoadPosition(bg->roadPositions[i]);
    }

    free(bg);
}

static void initMountains(Background* bg){
    bg->mountains[0] = MOUN_createMountain(1, 0);
    bg->mountains[1] = MOUN_createMountain(2, 45);

    bg->mountains[2] = MOUN_createMountain(1, 200);
    bg->mountains[3] = MOUN_createMountain(2, 245);
    bg->mountains[4] = MOUN_createMountain(1, bg->width - 100);
}

static void initRoadPositions(Background* bg){
    bg->roadPositions[0] = RP_createRoadPosition(0, 800, 130);
    bg->roadPositions[1] = RP_createRoadPosition(840, bg->width, 130);

    bg->roadPositions[2] = RP_createRoadPosition(30, 64, 300);
    bg->roadPositions[3] = RP_createRoadPosition(100, 160, 240);

    bg->roadPositions[4] = RP_createRoadPosition(300, 350, 180);



}







